package xin.data
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;

	/**
	 * 使用Proxy代理封装后的Object数据类
	 * @author xin
	 * 
	 */	
	public dynamic class ObjectBase extends Proxy implements IEventDispatcher
	{
		// 数据的属性个数
		private var _length:int;
		// 被封装的数据对象
		private var _dic:Dictionary;
		// 事件发送器
		private var _eventDispatcher:EventDispatcher;
		
		public function ObjectBase()
		{
			this._dic = new Dictionary();
			this._eventDispatcher = new EventDispatcher(this);
		}
		
		/**
		 * 重写设置属性的方法 
		 * @param	name 属性key
		 * @param	value 属性值
		 */
		override flash_proxy function setProperty(name:*, value:*):void 
		{
			if (!this._dic[name])
			{
				this._length ++;
			}
			this._dic[name] = value;
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		
		/**
		 * 重写删除属性的方法
		 * @param	name 属性key
		 * @return 若存在该属性, 则删除并返回true, 若属性不存在则返回false;
		 */
		override flash_proxy function deleteProperty(name:*):Boolean 
		{
			if (this._dic[name])
			{
				this._length --;
				var bl:Boolean = delete this._dic[name];
				this.dispatchEvent(new Event(Event.CHANGE));
				return bl;
			}
			return false;
		}
		
		/**
		 * 获取属性值
		 */
		override flash_proxy function getProperty(name:*):* 
		{
			return this._dic[name];
		}
		
		/**
		 * 判断属性是否存在
		 */
		override flash_proxy function hasProperty(name:*):Boolean 
		{
			return this._dic[name] != null;
		}
		
		/**
		 * 调用对象的public函数
		 * <p/> 内置clear()方法, 用于清空数据
		 * <p/> 内置forEach()方法, 用于遍历对象的所有属性, 使用方法: 
		 * <p/> var obj:ObjectBase() = new ObjectBase();
		 * <p/> obj.forEach(handler);  //handler只有两个参数
		 * <p/> function handler(key:String, value:Object):void{trace(key, value);};
		 * @param	name 函数名
		 * @param	...rest
		 */
		override flash_proxy function callProperty(name:*, ...rest):* 
		{
			switch (name.toString()) 
			{
				// 清空数据函数
				case "clear":
					this._length = 0;
					this._dic = new Dictionary();
				break;
				
				// 遍历数据执行
				case "forEach":
					for (var key:String in this._dic)
					{
						rest[0](key, _dic[key]);
					}
				break;
				
				// 默认
				default:
					if (this._dic[name])
					{
						return this._dic[name].apply(this._dic, rest);
					}
					else 
					{
						trace("未定义的函数" + name);
					}
				break;
			}
		}
		
		/**
		 * 重写 以 使ObjectBase可以使用for in 和 for each ..in
		 */		
		override flash_proxy function nextNameIndex(index:int):int
		{
			if (index < this._length)
			{
				return index + 1;
			}
			else
			{
				return 0;
			}
		}
		
		/**
		 * 数据属性的个数
		 */		
		public function get length():int
		{
			return this._length;
		}
		
		/////////////////////////////////////////////实现IEventDispatch接口
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			this._eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			this._eventDispatcher.removeEventListener(type, listener, useCapture);
		}
		
		public function dispatchEvent(event:Event):Boolean
		{
			return this._eventDispatcher.dispatchEvent(event);
		}
		
		public function hasEventListener(type:String):Boolean
		{
			return this._eventDispatcher.hasEventListener(type);
		}
		
		public function willTrigger(type:String):Boolean
		{
			return this._eventDispatcher.willTrigger(type);
		}
	}
}